<?php
namespace Application\Model;

use Application\Model\SejourTable;
use Application\Model\Sejour;
use Zend\Db\ResultSet\ResultSet;
use PHPUnit_Framework_TestCase;

class SejourTableTest extends PHPUnit_Framework_TestCase
{
    public function testFetchAllReturnsAllSejours()
    {
        $resultSet = new ResultSet();
        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
                                           array('select'), array(), '', false);
        $mockTableGateway->expects($this->once())
                         ->method('select')
                         ->with()
                         ->will($this->returnValue($resultSet));

        $sejourTable = new SejourTable($mockTableGateway);

        $this->assertSame($resultSet, $sejourTable->fetchAll());
    }
    
    public function testCanRetrieveAnSejourByItsId()
    {
    	$sejour = new Sejour();
    	$sejour->exchangeArray(array('id' => 123,
    								'titre' => 'toto'));
    	
    	$resultSet = new ResultSet();
    	$resultSet->setArrayObjectPrototype(new Sejour());
    	$resultSet->initialize(array($sejour));
    	
    	$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
                                           array('select'), array(), '', false);
    	$mockTableGateway->expects($this->once())
    					 ->method('select')
    					 ->with(array('id' => 123))
    					 ->will($this->returnValue($resultSet));
    	
    	$sejourTable = new SejourTable($mockTableGateway);
    	
    	$this->assertSame($sejour, $sejourTable->getSejour('123'));
    }
    
    public function testCanDeleteAnSejourByItsId()
    {
    	$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
    										array('delete'),array(), '', false);
    	$mockTableGateway->expects($this->once())
    					 ->method('delete')
    					 ->with(array('id' => 123));
    	$sejourTable = new SejourTable($mockTableGateway);
    	$sejourTable->deleteSejour(123);
    }
    
    public function testSaveSejourWillInsertNewSejoursIfTheyDontAlreadyHaveAnId()
    {
    	$sejour = new Sejour();
    	$sejourData = array(
    					   'titre' => 'toto');
    	$sejour->exchangeArray($sejourData);

    	$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
    				array('insert'),array(), '', false);    	
    	$mockTableGateway->expects($this->once())
				    	 ->method('insert')
				    	 ->with($sejourData);
    	
    	$sejourTable = new SejourTable($mockTableGateway);
    	$sejourTable->saveSejour($sejour);
    }
    
    public function testSaveSejourWillUpdateExistingSejoursIfTheyAlreadyHaveAnId()
    {
    	$sejour = new Sejour();
    	$sejour->exchangeArray(array('id' => 123,
    								'titre' => 'toto'));
    	
    	$resultSet = new ResultSet();
    	$resultSet->setArrayObjectPrototype(new Sejour());
    	$resultSet->initialize(array($sejour));
    	
    	$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
                                           array('select', 'update'), array(), '', false);
    	$mockTableGateway->expects($this->once())
    					 ->method('select')
    					 ->with(array('id' => 123))
    					 ->will($this->returnValue($resultSet));
		$mockTableGateway->expects($this->once())
    					 ->method('update')
    	    			 ->with(array('titre' => 'toto'),
    	    							array('id' => '123'));
    	
    	$sejourTable = new SejourTable($mockTableGateway);
    	$sejourTable->saveSejour($sejour);
    }    
    
	public function testExceptionIsThrownWhenGettingNonExistentSejour()
	{
	    $resultSet = new ResultSet();
	    $resultSet->setArrayObjectPrototype(new Sejour());
	    $resultSet->initialize(array());
	
	    $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
	    $mockTableGateway->expects($this->once())
	                     ->method('select')
	                     ->with(array('id' => 123))
	                     ->will($this->returnValue($resultSet));
	
	    $sejourTable = new SejourTable($mockTableGateway);
	
	    try {
	        $sejourTable->getSejour(123);
	    }
	    catch (\Exception $e) {
	        $this->assertSame('Could not find row 123', $e->getMessage());
	        return;
	    }
	
	    $this->fail('Expected exception was not thrown');
	}
    
    
}